package view;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;

import model.kwalificatiebeheer.Competentie;
import model.kwalificatiebeheer.Werkproces;
import java.awt.GridBagLayout;
import javax.swing.JTable;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.table.JTableHeader;

import controller.AddCompetentieToWerkprocesActions;
import javax.swing.JTextArea;
import javax.swing.JLabel;

public class AddCompetentieToWerkproces extends JFrame{

	private Werkproces wp;
	private Competentie activeCompetentie;
	private String activeComponent;
	private JPanel tablePanel;
	private JPanel formPanel;
	private JTable tblCompetenties;
	private JTable tblComponents;
	private ArrayList<String> components = new ArrayList<>();
	private JTextArea txtPrestatieIndicator;
	private String activePanel;
	
	public AddCompetentieToWerkproces(Werkproces wp){
		this.wp = wp;
		initTable();
		setTitle("Competentie Toevoegen");
		ImageIcon icon = new ImageIcon(this.getClass().getResource("../resources/favicon.png"));
		setIconImage(icon.getImage());
		setVisible(true);
		setSize(500, 500);		
	}
	
	public void initTable(){
		tablePanel = new JPanel();
		GridBagLayout gbl_tablePanel = new GridBagLayout();
		gbl_tablePanel.columnWidths = new int[]{0, 0, 0, 0};
		gbl_tablePanel.rowHeights = new int[]{0, 0, 0, 0, 0, 0};
		gbl_tablePanel.columnWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE};
		gbl_tablePanel.rowWeights = new double[]{0.0, 0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
		tablePanel.setLayout(gbl_tablePanel);
		
		tblCompetenties = new JTable(new CompetentieTableModel());
		tblCompetenties.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tblCompetenties.addMouseListener(new AddCompetentieToWerkprocesActions(this));
		
		JTableHeader head = tblCompetenties.getTableHeader();
		GridBagConstraints gbc_head = new GridBagConstraints();
		gbc_head.insets = new Insets(0, 0, 0, 0);
		gbc_head.fill = GridBagConstraints.BOTH;
		gbc_head.gridx = 1;
		gbc_head.gridy = 1;
		tablePanel.add(head, gbc_head);
		
		
		GridBagConstraints gbc_table = new GridBagConstraints();
		gbc_table.insets = new Insets(0, 0, 5, 0);
		gbc_table.fill = GridBagConstraints.BOTH;
		gbc_table.gridx = 1;
		gbc_table.gridy = 2;
		tablePanel.add(tblCompetenties, gbc_table);
		
		JButton btnSelecteren = new JButton("Selecteren");
		btnSelecteren.addActionListener(new AddCompetentieToWerkprocesActions(this));
		btnSelecteren.setActionCommand("btnSelect");
		GridBagConstraints gbc_btnSelecteren = new GridBagConstraints();
		gbc_btnSelecteren.anchor = GridBagConstraints.EAST;
		gbc_btnSelecteren.insets = new Insets(0, 0, 5, 5);
		gbc_btnSelecteren.gridx = 1;
		gbc_btnSelecteren.gridy = 3;
		tablePanel.add(btnSelecteren, gbc_btnSelecteren);
		
		setContentPane(tablePanel);
		activePanel = "tablePanel";
		revalidate();
	}
	
	public void initForm(){
		formPanel = new JPanel();
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, 1.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		formPanel.setLayout(gridBagLayout);
		
		JLabel lblPrestatieIndicator = new JLabel("Prestatie Indicator:");
		GridBagConstraints gbc_lblPrestatieIndicator = new GridBagConstraints();
		gbc_lblPrestatieIndicator.anchor = GridBagConstraints.WEST;
		gbc_lblPrestatieIndicator.insets = new Insets(0, 0, 5, 5);
		gbc_lblPrestatieIndicator.gridx = 1;
		gbc_lblPrestatieIndicator.gridy = 1;
		formPanel.add(lblPrestatieIndicator, gbc_lblPrestatieIndicator);
		
		txtPrestatieIndicator = new JTextArea();
		GridBagConstraints gbc_txtPrestatieIndicator = new GridBagConstraints();
		gbc_txtPrestatieIndicator.gridwidth = 2;
		gbc_txtPrestatieIndicator.insets = new Insets(0, 0, 5, 5);
		gbc_txtPrestatieIndicator.fill = GridBagConstraints.BOTH;
		gbc_txtPrestatieIndicator.gridx = 1;
		gbc_txtPrestatieIndicator.gridy = 2;
		formPanel.add(txtPrestatieIndicator, gbc_txtPrestatieIndicator);
		
		JLabel lblComponenten = new JLabel("Componenten:");
		GridBagConstraints gbc_lblComponenten = new GridBagConstraints();
		gbc_lblComponenten.anchor = GridBagConstraints.WEST;
		gbc_lblComponenten.insets = new Insets(0, 0, 5, 5);
		gbc_lblComponenten.gridx = 1;
		gbc_lblComponenten.gridy = 3;
		formPanel.add(lblComponenten, gbc_lblComponenten);
		
		tblComponents = new JTable();
		tblComponents.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tblComponents.addMouseListener(new AddCompetentieToWerkprocesActions(this));
		GridBagConstraints gbc_tblComponents = new GridBagConstraints();
		gbc_tblComponents.gridwidth = 2;
		gbc_tblComponents.insets = new Insets(0, 0, 5, 5);
		gbc_tblComponents.fill = GridBagConstraints.BOTH;
		gbc_tblComponents.gridx = 1;
		gbc_tblComponents.gridy = 4;
		formPanel.add(tblComponents, gbc_tblComponents);
		
		JButton btnComponentVerwijderen = new JButton("Component Verwijderen");
		btnComponentVerwijderen.addActionListener(new AddCompetentieToWerkprocesActions(this));
		btnComponentVerwijderen.setActionCommand("deleteComp");
		GridBagConstraints gbc_btnComponentVerwijderen = new GridBagConstraints();
		gbc_btnComponentVerwijderen.anchor = GridBagConstraints.WEST;
		gbc_btnComponentVerwijderen.insets = new Insets(0, 0, 5, 5);
		gbc_btnComponentVerwijderen.gridx = 1;
		gbc_btnComponentVerwijderen.gridy = 5;
		formPanel.add(btnComponentVerwijderen, gbc_btnComponentVerwijderen);
		
		JButton btnComponentToevoegen = new JButton("Component Toevoegen");
		btnComponentToevoegen.addActionListener(new AddCompetentieToWerkprocesActions(this));
		btnComponentToevoegen.setActionCommand("addComp");
		GridBagConstraints gbc_btnComponentToevoegen = new GridBagConstraints();
		gbc_btnComponentToevoegen.anchor = GridBagConstraints.EAST;
		gbc_btnComponentToevoegen.insets = new Insets(0, 0, 5, 5);
		gbc_btnComponentToevoegen.gridx = 2;
		gbc_btnComponentToevoegen.gridy = 5;
		formPanel.add(btnComponentToevoegen, gbc_btnComponentToevoegen);
		
		JButton btnOpslaan = new JButton("Opslaan");
		btnOpslaan.addActionListener(new AddCompetentieToWerkprocesActions(this));
		btnOpslaan.setActionCommand("btnSave");
		GridBagConstraints gbc_btnOpslaan = new GridBagConstraints();
		gbc_btnOpslaan.anchor = GridBagConstraints.EAST;
		gbc_btnOpslaan.insets = new Insets(0, 0, 5, 5);
		gbc_btnOpslaan.gridx = 2;
		gbc_btnOpslaan.gridy = 6;
		formPanel.add(btnOpslaan, gbc_btnOpslaan);
		
		setContentPane(formPanel);
		activePanel = "formPanel";
		revalidate();
	}
	
	public Competentie getActiveCompetentie() {
		return activeCompetentie;
	}

	public void setActiveCompetentie(Competentie activeCompetentie) {
		this.activeCompetentie = activeCompetentie;
	}

	public Werkproces getWp() {
		return wp;
	}

	public JPanel getTablePanel() {
		return tablePanel;
	}

	public JPanel getFormPanel() {
		return formPanel;
	}

	public JTable getTblCompetenties() {
		return tblCompetenties;
	}

	public JTable getTblComponents() {
		return tblComponents;
	}

	public void setTblComponents(JTable tblComponents) {
		this.tblComponents = tblComponents;
	}

	public ArrayList<String> getComponentsCriterium() {
		return components;
	}

	public void setComponents(ArrayList<String> components) {
		this.components = components;
	}

	public JTextArea getTxtPrestatieIndicator() {
		return txtPrestatieIndicator;
	}

	public void setTxtPrestatieIndicator(JTextArea txtPrestatieIndicator) {
		this.txtPrestatieIndicator = txtPrestatieIndicator;
	}

	public void setWp(Werkproces wp) {
		this.wp = wp;
	}

	public void setTblCompetenties(JTable tblCompetenties) {
		this.tblCompetenties = tblCompetenties;
	}

	public String getActivePanel() {
		return activePanel;
	}

	public void setActivePanel(String activePanel) {
		this.activePanel = activePanel;
	}

	public String getActiveComponent() {
		return activeComponent;
	}

	public void setActiveComponent(String activeComponent) {
		this.activeComponent = activeComponent;
	}
}
